//
//  WLHomeViewController.swift
//  WLWeibo
//
//  Created by Devil on 16/8/11.
//  Copyright © 2016年 Wiliam. All rights reserved.
//

import UIKit

private let WLHomeViewCellId = "WLHomeViewCellId"
class WLHomeViewController: WLVisitorViewController {

    private lazy var statusListViewModel: WLStatusListViewModel = WLStatusListViewModel()
    let refreshControl = WLRefreshControl()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if  !isLogin{
            
            visitorView?.setupVisitorViewInfo(nil, title: nil)
            return
            
        }
        
        setupTableViewInfo()
//        statusListViewModel.loadData { (isSuccess) in
//            if !isSuccess {
//            
//                print("请求数据失败")
//                return
//            
//            }
//            
//            print("请求数据成功")
//            self.tableView.reloadData()
//        }

        loadData()
    
    }
    

    
    func setupTableViewInfo(){
        //注册Cell
        tableView.registerClass(WLStatusCell.self, forCellReuseIdentifier: WLHomeViewCellId)
        //设置代理
        tableView.dataSource = self
        tableView.delegate = self
        //设置高度
        tableView.rowHeight = UITableViewAutomaticDimension
        tableView.estimatedRowHeight = 100
        //设置footView
        tableView.tableFooterView = activityView
        //添加事件
        refreshControl.addTarget(self, action: "refreshAction", forControlEvents: UIControlEvents.ValueChanged)
        //添加到tableView上
        tableView.addSubview(refreshControl)
        tableView.separatorStyle = .None
    }
    
    func refreshAction(){
        printLog("请求数据")
        loadData()
    }
    //菊花
    private lazy var activityView: UIActivityIndicatorView = {
        let view = UIActivityIndicatorView(activityIndicatorStyle: UIActivityIndicatorViewStyle.WhiteLarge)
        view.color = AppThemeColor
        return view
    
    }()
}

extension WLHomeViewController: UITableViewDataSource,UITableViewDelegate {

    func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return statusListViewModel.dataArray.count
    }
    
    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(WLHomeViewCellId, forIndexPath: indexPath) as! WLStatusCell
        cell.statusViewModel = statusListViewModel.dataArray[indexPath.row]
        return cell
    }
    
    func tableView(tableView: UITableView, willDisplayCell cell: UITableViewCell, forRowAtIndexPath indexPath: NSIndexPath) {
        if indexPath.row + 1 == statusListViewModel.dataArray.count && activityView.isAnimating() {
            printLog(indexPath.row)
            //设置动画
            activityView.startAnimating()
            //请求数组
            loadData()
        }
    }
}

extension WLHomeViewController {

    func loadData(){
    
        statusListViewModel.loadData(activityView.isAnimating()) { (isSuccess) in
            self.endRefeshing()
            if !isSuccess{
                print("请求数据失败")
                return
            
            }
            
            print("请求数据成功")
            //刷新UI
            self.tableView.reloadData()
        }
    
    }
    //停止动画
    func endRefeshing(){
        self.activityView.stopAnimating()
        self.refreshControl.endRefreshing()
    }
}
